public class FindPeakElement {
    // 寻找峰值
    // https://www.nowcoder.com/practice/fcf87540c4f347bcb4cf720b5b350c76?tpId=295&tqId=2227748&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
    public int findPeakElement (int[] nums) {
        // write code here
        int len = nums.length;
        int left = 0, right = nums.length-1;
        if (nums.length == 1 || nums[0] > nums[1]) {
            return 0;
        }
        if (nums[len-1] > nums[len-2]) {
            return len-1;
        }
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]) {
                return mid;
            } else if (nums[mid] < nums[mid+1]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
}
